using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Report.PhieuD
{
    public partial class UcD96 : DevExpress.XtraEditors.XtraUserControl
    {
        public UcD96()
        {
            InitializeComponent();
        }

        private void UcD96_Load(object sender, EventArgs e)
        {
            DataTable dtD96 = new DsReportX.Table7ColDataTable();
            dtD96 = CalculateData(dtD96);
            gridControlA.DataSource = dtD96;
        }

        DataTable CalculateData(DataTable dt)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                cmd.CommandText = "SELECT ChiTra2011 Number1, CONVERT(MONEY, 100.0 * ChiTra2011/TongChiTra2011) Number2,\n" +
                                              "ChiTra2012 Number3 , CONVERT(MONEY, 100.0 * ChiTra2012/TongChiTra2012) Number4\n" +
                                              "FROM (\n" +
                                              " SELECT SUM(Number1) ChiTra2011,  SUM(Number3) ChiTra2012\n" +
                                              " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID  \n" +
                                              " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 10 \n" +
                                              " GROUP BY ThuTuTrongBang\n" +
                                              " UNION ALL\n" +
                                              " SELECT SUM(Number1) TongChiTra2011,  SUM(Number3) TongChiTra2012\n" +
                                              " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID \n" +
                                              " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 10 \n" +
                                              ") ChiTra, (\n" +
                                              " SELECT SUM(Number1) TongChiTra2011,  SUM(Number3) TongChiTra2012\n" +
                                              " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID \n" +
                                              " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 10 \n" +
                                              ") TongChiTra ";

                da.SelectCommand = cmd;
                da.Fill(dt);

                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
